package com.excerise.queue;

import java.util.ArrayList;
import java.util.List;

/**
 *
 *
 * @author Way on 2021/10/9
 */
public class Queue {

    /**
     * 桌
     */
    private final ArrayList<Table> tables = new ArrayList<>(5);

    public Queue() {
        tables.add(new Table());
        tables.add(new Table());
        tables.add(new Table());
        tables.add(new Table());
        tables.add(new Table());
    }

    /**
     * 是否满座
     * @return
     */
    public boolean isFull() {
        int full = -1;
        for (Table table : tables) {
            if (table.isFull()) {
                full++;
            }
        }
        return full == 4;
    }

    public void in() {
        int num = 9;
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            list.add(i);
        }

        int tableIndex = 0;
        int seatIndex = 0;
        do {
            Table table = tables.get(tableIndex);
            // 入座
            if (!table.isFull()) {
                table.push(list.get(seatIndex));
                seatIndex++;
            } else {
                tableIndex++;
            }
        } while (!isFull() && seatIndex != list.size());

        // 遍历当前状态
        for (Table table : tables) {
            table.list();
        }
    }

    public static void main(String[] args) {
        Queue queue = new Queue();
        queue.in();
    }
}
